#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2016-2018, Niklas Hauser
# Copyright (c) 2017, Fabian Greif
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

description = """
# STM32F030 Demo Board

STM32F030F4P6 Minimum System Development Board.

Cheap and bread-board-friendly board for STM32 F0 series.
Sold for less than 1.5 USD on well known Internet shops from China.

https://stm32-base.org/boards/STM32F030F4P6-STM32F030-DEMO-BOARD-V1.1

## Programming

Since the board doesn't have a programmer on-board, you need to use your
own and *specify* which one you're using in a custom `openocd.cfg` file:

```
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]

# To select a specific programmer you can specify its serial number
#hla_serial "\\x53\\x3f\\x6f\\x06\\x51\\x77\\x50\\x57\\x12\\x17\\x13\\x3f"
# You can discover the serial via `stlink --hla-serial` or `st-info --hla-serial`.
```

Then include this file in your build options like so:

```xml
<library>
  <extends>modm:stm32f030_demo</extends>
  <options>
    <option name="modm:build:openocd.cfg">openocd.cfg</option>
  </options>
</library>
```
"""

def init(module):
    module.name = ":board:stm32f030_demo"
    module.description = description

def prepare(module, options):
    if not options[":target"].partname.startswith("stm32f030f4p"):
        return False

    module.depends(
        ":architecture:clock",
        ":platform:clock",
        ":platform:core",
        ":platform:gpio")

    return True

def build(env):
    env.outbasepath = "modm/src/modm/board"
    env.substitutions = {
        "with_logger": False,
        "with_assert": env.has_module(":architecture:assert")
    }
    env.template("../board.cpp.in", "board.cpp")
    env.copy('.')

    env.outbasepath = "modm/openocd/modm/board/"
    env.copy(repopath("tools/openocd/modm/stm32f030_demo_board.cfg"), "stm32f030_demo_board.cfg")
    env.collect(":build:openocd.source", "modm/board/stm32f030_demo_board.cfg")

    # Warn the user if they forgot to set a custom openocd config
    if env.has_option(":build:openocd.cfg") and not len(env.get(":build:openocd.cfg", "")):
        env.log.warning("You need to provide the programmer via a custom OpenOCD config!\n" + description)
